package com.shujia

// 定义父类：PersonExtend
class PersonExtend(name: String, gender: String, age: Int) {
  val _name: String = name
  val _gender: String = gender
  val _age: Int = age

  // 打印基本的信息：姓名、性别、年龄
  def printStandInfo(): Unit = {
    println(s"${_name},${_gender},${_age}")
  }

}

// 定义学生类StudentExtend
// 在Scala中继承父类的同时需要调用父类的构造方法
// 继承可以自动获得父类的属性以及方法
class StudentExtend(sid: String, name: String, gender: String, age: Int) extends PersonExtend(name, gender, age) {
  val _sid: String = sid

  def Study(): Unit = {
    println("学生会学习")
  }

  // 重写父类方法
  // 打印所有的信息：学生id、姓名、性别、年龄
  override def printStandInfo(): Unit = {
    println(s"Student INFO: ${_sid},${_name},${_gender},${_age}")
  }

}

// 定义老师类TeacherExtend
class TeacherExtend(tid: String, name: String, gender: String, age: Int) extends PersonExtend(name, gender, age) {
  val _tid: String = tid

  def Teaching(): Unit = {
    println("老师会上课")
  }

  // 重写父类方法
  // 打印所有的信息：老师id、姓名、性别、年龄
  override def printStandInfo(): Unit = {
    println(s"Teacher INFO: ${_tid},${_name},${_gender},${_age}")
  }

}

object Demo03ClassExtend {
  //  // 对学生类的对象 对打印方法调用两次
  //  def personPrintTwice(p:StudentExtend):Unit = {
  //    p.printStandInfo()
  //    p.printStandInfo()
  //  }
  //
  //  // 对老师类的对象 对打印方法调用两次
  //  def personPrintTwice(p:TeacherExtend):Unit = {
  //    p.printStandInfo()
  //    p.printStandInfo()
  //  }

  // 对Person类的对象 对打印方法调用两次
  def personPrintTwice(p: PersonExtend): Unit = {
    p.printStandInfo()
    p.printStandInfo()
  }

  def main(args: Array[String]): Unit = {
    val stu01: StudentExtend = new StudentExtend("1500100002", "吕金鹏", "男", 20)
    println(stu01._sid)
    println(stu01._name)
    stu01.Study()
    stu01.printStandInfo()

    val teach01: TeacherExtend = new TeacherExtend("0001", "张老师", "男", 28)
    println(teach01._tid)
    println(teach01._name)
    teach01.Teaching()
    teach01.printStandInfo()

    // 多态：父类引用指向子类对象
    Demo03ClassExtend.personPrintTwice(stu01)
    Demo03ClassExtend.personPrintTwice(teach01)
  }

}
